<?php


namespace Database\Seeders;


use App\Constants\AdvConfigConstant;
use App\Constants\MemberTaskConstant;
use App\Constants\MiniProgramConstant;
use App\Constants\SignConstant;
use App\Constants\SmsConstant;
use App\Models\MemberTask;
use App\Models\MiniProgram;
use App\Models\Settings;
use App\Models\SmsTpl;
use App\Modules\Health\Models\HealthConfigs;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class V120Seeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        try {
            DB::beginTransaction();

            $this->initMemberTask();
            $this->initSettings();
            $this->initSmsTpl();
//            $this->initMiniProgram();
            $this->initHealth();

            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            log_exception('1.2版本数据填充失败', $e);
            dd($e->getMessage());
        }
    }

    protected function initMemberTask()
    {
        $data = [];

        $map = MemberTaskConstant::MAP;

        foreach (MiniProgramConstant::MAP as $k => $v) {
            foreach ($map as $key => $item) {
                $data[] = [
                    'slug'       => $key,
                    'remark'     => $item,
                    'mini_type'  => $k,
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ];
            }
        }


        MemberTask::insert($data);
    }

    protected function initSettings()
    {
        $data = [];

        /*
        $advGroup = AdvConfigConstant::ADV_GROUP;
        $advMap = AdvConfigConstant::MAP;

        foreach (MiniProgramConstant::MAP as $k => $v) {
            foreach ($advMap as $name => $remark) {
                $data[] = [
                    'name'       => $name,
                    'group'      => $advGroup,
                    'remark'     => $remark,
                    'mini_type'  => $k,
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ];
            }
        }

        $signGroup = SignConstant::GROUP;
        $signMap = SignConstant::MAP;

        foreach (MiniProgramConstant::MAP as $k => $v) {
            foreach ($signMap as $name => $remark) {
                $data[] = [
                    'name'       => $name,
                    'group'      => $signGroup,
                    'remark'     => $remark,
                    'mini_type'  => $k,
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ];
            }
        }

        $wechatGroup = MiniProgramConstant::GROUP;
        $wechatMap = MiniProgramConstant::SETTING_MAP;

        foreach (MiniProgramConstant::MAP as $k => $v) {
            foreach ($wechatMap as $name => $remark) {
                $data[] = [
                    'name'       => $name,
                    'group'      => $wechatGroup,
                    'remark'     => $remark,
                    'mini_type'  => $k,
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ];
            }
        }
        */
        $smsGroup = SmsConstant::GROUP;
        $smsMap = SmsConstant::MAP;

        foreach ($smsMap as $name => $remark) {
            $data[] = [
                'name'       => $name,
                'group'      => $smsGroup,
                'remark'     => $remark,
                'mini_type'  => '',
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ];
        }

        Settings::insert($data);
    }

    protected function initSmsTpl()
    {
        $data = [];

        foreach (SmsConstant::TPL_TYPE_MAP as $type => $title) {
            $data[] = [
                'type'       => $type,
                'title'      => $title,
                'content'    => '',
                'template'   => '',
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ];
        }

        SmsTpl::insert($data);
    }

    protected function initMiniProgram()
    {
        $data = [];

        foreach (MiniProgramConstant::MAP as $slug => $name) {
            $data[] = [
                'name'       => $name,
                'slug'       => $slug,
                'status'     => true,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ];
        }

        MiniProgram::insert($data);
    }

    protected function initHealth()
    {
        $data = [];

        $advGroup = \App\Modules\Health\Constants\AdvConfigConstant::GROUP;
        $advMap = \App\Modules\Health\Constants\AdvConfigConstant::MAP;

        foreach ($advMap as $name => $remark) {
            $data[] = [
                'name'       => $name,
                'group'      => $advGroup,
                'remark'     => $remark,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ];
        }

        HealthConfigs::insert($data);
    }
}
